Thread: [c+Win32] Windows/Dialogs Questions

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    31

    [c+Win32] Windows/Dialogs Questions

    i am learning about win32 programming.

    i came around the code below and it got me having unanswered questions.

    Code:
    #include <windows.h>
    
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    LRESULT CALLBACK DialogProc(HWND, UINT, WPARAM, LPARAM);
    
    void CreateDialogBox(HWND);
    void RegisterDialogClass(HWND);
    
    
    HINSTANCE ghInstance;
    
    int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow){
        MSG msg;
        HWND hwnd;
    
        WNDCLASSW wc = {0};
    
        wc.lpszClassName = L"Window";
        wc.hInstance = hInstance;
        wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
        wc.lpfnWndProc = WndProc;
    
        RegisterClassW(&wc);
        hwnd = CreateWindowW(wc.lpszClassName, L"Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 250, 150, NULL, NULL, hInstance, NULL);
        ghInstance = hInstance;
    
        while(GetMessage(&msg, NULL, 0,0)){
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return (int)msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
        switch(msg){
            case WM_CREATE:
                RegisterDialogClass(hwnd);
                CreateWindowW(L"Button", L"Show Dialog", WS_VISIBLE | WS_CHILD, 20, 20, 95, 25, hwnd, (HMENU)1, NULL, NULL);
                break;
            case WM_COMMAND:
                CreateDialogBox(hwnd);
                break;
            case WM_DESTROY:
            {
                PostQuitMessage(0);
                break;
            }
        }
        return (DefWindowProcW(hwnd, msg, wParam, lParam));
    }
    
    LRESULT CALLBACK DialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
        switch(msg){
            case WM_CREATE:
                CreateWindowW(L"Button", L"Ok", WS_VISIBLE | WS_CHILD, 50, 50, 80, 25, hwnd, (HMENU) 1, NULL, NULL);
                break;
            case WM_COMMAND:
                DestroyWindow(hwnd);
                break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
                break;
        }
    
        return (DefWindowProcW(hwnd, msg, wParam, lParam));
    }
    
    void RegisterDialogClass(HWND hwnd){
        WNDCLASSEXW wc = {0};
        wc.cbSize = sizeof(WNDCLASSEXW);
        wc.lpfnWndProc = (WNDPROC)DialogProc;
        wc.hInstance = ghInstance;
        wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
        wc.lpszClassName = L"DialogClass";
        RegisterClassExW(&wc);
    
    }
    
    void CreateDialogBox(HWND hwnd){
        CreateWindowExW(WS_EX_DLGMODALFRAME | WS_EX_TOPMOST, L"DialogClass", L"Dialog Box", WS_VISIBLE | WS_SYSMENU | WS_CAPTION, 100, 100, 200, 150, NULL, NULL, ghInstance, NULL);
    }

    the code works properly but i have questions;

    1. the code line
    Code:
    void CreateDialogBox(HWND);
    void RegisterDialogClass(HWND);
    declares two functiions which both return nothing but take as argument handle to a window. however in both functions definition, there is no place where the handle argument which they receive(hwnd) is used. what i'm i missing?

    2. in the code line
    Code:
    wc.lpfnWndProc = (WNDPROC)DialogProc;
    the dialog box's procedure function address is type casted to type 'WNDPROC' - note that i'm using my knowledge of data-type casting here but i never knew same thing applied to functions too. if they do, is 'WNDPROC' a type of function? what i'm i missing?

    3. according to msdn, for the WNDCLASSEX structure;
    hInstance
    Type: HINSTANCE

    A handle to the instance that contains the window procedure for the class.
    but in this code we have

    Code:
    wc.hInstance = ghInstance;
    but ghInstance has been assigned the value of the main program's hInstance variable. And the windows procedure contained in main program is WndProc according to

    Code:
    wc.lpfnWndProc = WndProc;
    could it mean that that was why they
    "function-type-casted" 'DialogProc;' to '(WNDPROC)DialogProc;' ???

    i'm confused. Please pardon this beginner questions.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,114
    This forum is for the generic discussion of the C Programming language itself, not Win32 programming.

    Perhaps you would receive quicker and better responses on the Windows Programming forum on this site.

    Thank you!

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    These are general C questions for the most part.

    1. You seem to be asking why the person who wrote the code passed in parameters that aren't used. I really don't know. Brain damage? You would have to ask their therapist. On the other hand, the 4th-last parameter to CreateWindowEx is the parent window handle, so it could be used there instead of just passing NULL.

    2. Functions have types just like variables. You can, for instance, pass a function to another function (which is exactly what you're doing in passing your WndProc and DialogProc functions as a "callback" functions). The function name is essentially a pointer to the function's starting point.

    Code:
    #include <stdio.h>
    
    // f is a pointer to a function that takes 2 ints and returns an int.
    int func(int (*f) (int, int), int a, int b) {
        return f(a, b);
    }
    
    int add(int a, int b) { return a + b; }
    
    int mul(int a, int b) { return a * b; }
    
    int main() {
        printf("%d\n", func(add, 3, 5));  // prints 8
        printf("%d\n", func(mul, 3, 5));  // prints 15
        return 0;
    }
    You can even typedef the type:
    Code:
    typedef int (* MathFunc ) (int, int)
    
    int func(MathFunc f, int a, int b) {
        return f(a, b);
    }
    3. This question is pretty much incomprehensible. An "instance" is a running program. I don't see what you're asking, but it seems to have nothing to do with the typecast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding external windows to dialogs [how?]
    By RedZone in forum Windows Programming
    Replies: 0
    Last Post: 12-18-2005, 06:23 PM
  2. Windows Forms and/or MFC [But definately not win32!] questions
    By Robert_Sitter in forum Windows Programming
    Replies: 2
    Last Post: 12-03-2005, 09:28 PM
  3. Some newbish questions about dialogs and tabs
    By Longie in forum Windows Programming
    Replies: 0
    Last Post: 09-02-2005, 08:09 PM
  4. Win32 API Questions
    By CPP-Null in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2003, 04:26 PM
  5. dialogs vs windows
    By cppdude in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2002, 02:30 PM

Tags for this Thread